var g_jsonMap = null;
// json map layer
var JsonMap = cc.Node.extend({
    _awards: null,
    _directs: null,
    _childs: null,
    ctor: function () {
        this._super();
        g_jsonMap=this;
        g_jsonMap.retain();
        this._awards = new Array();
        this._directs = new Array();
        this._childs = new Array();
        return true;
    },
    onEnter: function () {
        this._super();
    },
    onExit: function () {
        this._awards.splice(0, this._awards.length);
        this._directs.splice(0, this._directs.length);
        this._childs.splice(0, this._childs.length);
        this._super();
    },
    initWithJsonFile: function (filename) {
        if (filename) {
            var jsonObject = CreateJsonWithFile(filename);
            if (jsonObject) {
                cc.log("initialize json map from file: " + filename);
                this.layoutMapElements(jsonObject);
                this.layoutRoleFlag(jsonObject);
                this.layoutEmitterFlag(jsonObject);
                return true;
            } else {
                cc.log("load json fail,please check your json file: " + filename);
                return false;
            }
        } else {
            cc.log("error: " + filename + " json file name should not be null");
            return false;
        }
    },
    initWithJsonObject: function (jsonobject) {
        var jsonObject = jsonobject;
        if (jsonObject) {
            cc.log("initialize json map from json object: " + jsonObject);
            this.layoutMapElements(jsonObject);

            this.layoutRoleFlag(jsonObject);
            this.layoutEmitterFlag(jsonObject);

            return true;
        } else {
            cc.log("invalid json map object: " + jsonObject);
            return false;
        }
    },
    // layout map elements
    layoutMapElements: function (jsonobject) {
        cc.log("layoutMapElements");
        var jsonObject = jsonobject;
        if (jsonObject) {
            cc.log("layout map elements:");
            var tileWidth = jsonObject.width;
            var tileHeight = jsonObject.height;
            for (var x = 0; x < jsonObject.xGrid; x++) {
                var yArray = [];
                for (var y = 0; y < jsonObject.yGrid; y++) {
                    var  haveChild = false; //objArray = [],
                    for (var i = 0; i < jsonObject.children.length; i++) {
                        var child = jsonObject.children[i];
                        if (child.x == x && child.y == y) {
                            // have child in (x,y)
                            haveChild = true;
                            // award
                            var childObj = null;
                            if (child.hp) {
                                var filename = MapResource(child.res);
                                var childObj = new JsonAward(filename, child.hp, child.gold);
                                childObj.setPosition(cc.p(tileWidth * (x + 0.5), tileHeight * (y + 0.5)));
                                childObj.setPoint( cc.p(x,y) );// childObj.setTag(this._awards.length);
                                this.addChild(childObj, -child.y + 1,this._awards.length);
                                var awards = [];
                                awards.push(childObj);
                                awards.push(x);
                                awards.push(y);
                                this._awards.push(awards);

                            }
                            // road
                            else {
                                var filename = MapResource(child.res);
                                childObj = new JsonGrid(filename, child.d ? child.d : 0);
                                childObj.setPosition(cc.p(tileWidth * (x + 0.5), tileHeight * (y + 0.5)));
                                this.addChild(childObj);
                                // direct
                                if (child.d) {
                                    /*var directs = [];
                                    directs.push(childObj);
                                    directs.push(x);
                                    directs.push(y);//*/
                                    this._directs.push(childObj);
                                    cc.log("direct at (" + x + "," + y + ")");
                                }
                            }
                            yArray.push(childObj);
                            cc.log("have child (" + x + "," + y + ") object array length: " + yArray.length);
                            break;
                        }
                    }
                    // not have child in (x,y)
                    if (!haveChild) {
                        yArray.push( null );
                        cc.log("not child (" + x + "," + y + ") object array length: " + yArray.length);
                    }
                }
                this._childs.push(yArray);
            }
        } else {
            cc.log("json object invalid");
        }
    },
    layoutRoleFlag:function(jsonobject){
        cc.log("layoutRoleFlag");
        var jsonObject = jsonobject;
        if(jsonObject){
            cc.log("layout role:");
            var rol = jsonObject.chars[0];
            var filename = MapResource(rol.res);
            var emitter = new Role(filename);
            //emitter.setAnchorPoint(cc.p(0.5,0));
            emitter.setPosition(cc.p(rol.x+jsonObject.width*0.5, rol.y+jsonObject.height*0.2));
            this.addChild(emitter);
        }else{
            cc.log("json object invalid");
        }
    },
    layoutEmitterFlag:function(jsonobject){
        // emitter
        cc.log("layoutEmitterFlag");
        var jsonObject = jsonobject;
        if(jsonObject){
            cc.log("layout emitter:");
            var emit = jsonObject.chars[1];
            var filename = MainResource(emit.res);
            var emitter = new cc.Sprite(filename);
            emitter.setPosition(cc.p(emit.x, emit.y));
            this.addChild(emitter);
        }else{
            cc.log("json object invalid");
        }
    },
    // get child buffer
    getChildArray: function () {
        //cc.log("get child array");
        return this._childs;
    },
    getAwardArray: function () {
        //cc.log("get award array");
        return this._awards;
    },
    getDirectArray: function () {
        //cc.log("get direct array");
        return this._directs;
    },
    // get child at position
    getChildAtPoint : function(x,y){
        if(x>=0&&x<this._childs.length){
            var arr = this._childs[x];
            if(y>=0&&y<arr.length){
                return this._childs[x][y];
            }
        }
        cc.log("no child at x: "+x+" y: "+y);
        return null;
    },
    getChildAtPoint : function(point){
        if(point.x>=0&&point.x<this._childs.length){
            var arr = this._childs[point.x];
            if(point.y>=0&&point.y<arr.length){
                return this._childs[point.x][point.y];
            }
        }
        cc.log("no child at x: "+point.x+" y: "+point.y);
        return null;
    },
    // checking point is empty
    isEmptyPoint : function(x,y){
        if(null==this.getChildAtPoint(x,y)){
            return true;
        }
        cc.log("have child at x: "+x+" y: "+y);
        return false;
    },
    isEmptyPoint : function(point){
        if(null==this.getChildAtPoint(point)){
            return true;
        }
        cc.log("have child at x: "+point.x+" y: "+point.y);
        return false;
    },
    register : function(x,y,registerobj){
        if(x>=0&&x<this._childs.length){
            var arr = this._childs[x];
            if(y>=0&&y<arr.length){
                cc.log("register at x: "+x+" y: "+y);
                this._childs[x][y]=registerobj;
                return true;
            }
        }
        cc.log("no child at x: "+x+" y: "+y);
        return false;
    },
    register1 : function(point,registerobj){
        if(point.x>=0&&point.x<this._childs.length){
            var arr = this._childs[point.x];
            if(point.y>=0&&point.y<arr.length){
                cc.log("register at x: "+point.x+" y: "+point.y);
                this._childs[point.x][point.y]=registerobj;
                return true;
            }
        }
        cc.log("no child at x: "+point.x+" y: "+point.y);
        return false;
    },
    unregister : function(x,y){
        if(x>=0&&x<this._childs.length){
            var arr = this._childs[x];
            if(y>=0&&y<arr.length){
                cc.log("unregister at x: "+x+" y: "+y);
                this._childs[x][y]=null;
                return true;
            }
        }
        cc.log("no child at x: "+x+" y: "+y);
        return false;
    },
    unregister1 : function(point){
        if(point.x>=0&&point.x<this._childs.length){
            var arr = this._childs[point.x];
            if(point.y>=0&&point.y<arr.length){
                cc.log("unregister at x: "+point.x+" y: "+point.y);
                this._childs[point.x][point.y]=null;
                return true;
            }
        }
        cc.log("no child at x: "+point.x+" y: "+point.y);
        return false;
    }
});
JsonMap.createWithJsonFile=function(filename){
    cc.log("create json map from file: "+filename);
    var jsonMap = new JsonMap();
    jsonMap.initWithJsonFile(filename);
    return jsonMap;
}
JsonMap.createWithJsonObject=function(jsonobject){
    cc.log("create json map from json object: "+jsonobject);
    var jsonMap = new JsonMap();
    jsonMap.initWithJsonObject(jsonobject);
    return jsonMap;
}
////////////////////////////////////////////////////////////
GetJsonMap=function(){
    return g_jsonMap;
}
MapEmptyPoint=function( point ){
    if( !PointInMapRect( point ) ) return false;
    return g_jsonMap.isEmptyPoint( point );
}
